home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / extras / programm / gemfsc20 / gemfsc20.lzh / EXAMPLES / BROWSE / BRFILE.C < prev    next >
Text File  |  1992-12-07  |  5KB  |  202 lines

  1. /*****************************************************************************
  2.  * BRFILE - Load a file into a browser window.
  3.  ****************************************************************************/
  4.  
  5. #include <osbind.h>
  6.  
  7. #define BROWSER_INTERNALS
  8. #include "browser.h"
  9.  
  10. #ifndef NULL
  11.   #define NULL ((void*)0)
  12. #endif
  13.  
  14. #ifndef TRUE
  15.   #define TRUE    1
  16.   #define FALSE 0
  17. #endif
  18.  
  19. #define IOBUFSIZE          16384
  20. #define IOBUFMIN            512
  21. #define MAX_LINE_LENGTH     512
  22. #define MY_EOF                 -1
  23.  
  24. #define DosMalloc(a)   ((void*)Malloc((long)a))
  25.  
  26. typedef short Boolean;
  27. typedef unsigned char uchar;
  28.  
  29. static short   fhandle;
  30. static uchar   *iobuf;
  31. static long    buf_alloc_size;
  32. static long    buf_filled_size;
  33. static long    buf_cur_idx;
  34.  
  35. static void my_ungetc(c)
  36. /*****************************************************************************
  37.  *
  38.  ****************************************************************************/
  39.     int c;
  40. {
  41.     if (MY_EOF != c) {                /* don't push back EOF chars, they   */
  42.         ++buf_filled_size;            /* would come back as 0xFF, not EOF. */
  43.         iobuf[--buf_cur_idx] = c;    /* by not pushing them back, iobuf     */
  44.     }                                /* stays empty, and EOF is returned  */
  45. }                                    /* on the next getc() call.          */
  46.  
  47. static int my_getc()
  48. {
  49.     if (0 >= buf_filled_size) {
  50.         buf_cur_idx = 0;
  51.         if (0 >= (buf_filled_size = Fread(fhandle, buf_alloc_size, iobuf)))
  52.             return MY_EOF;
  53.     }
  54.     --buf_filled_size;
  55.     return iobuf[buf_cur_idx++];
  56. }
  57.  
  58. static Boolean build_line(pbuf)
  59. /*****************************************************************************
  60.  *
  61.  ****************************************************************************/
  62.     register uchar *pbuf;
  63. {
  64.     register int c;
  65.     register int count = 0;
  66.  
  67.     for (;;) {
  68.         if (count >= MAX_LINE_LENGTH-2)
  69.             goto LINE_END;
  70.         if (' ' <= (c = my_getc())) {       /* if it's a normal char */
  71.             *pbuf++ = c;                    /* just pass it thru     */
  72.             ++count;
  73.         } else {                            /* else see if it needs   */
  74.             switch (c) {                    /* any special handling   */
  75.  
  76.               case MY_EOF:                    /* end of file */
  77.  
  78.                 if (count) {                /* if any chars on line,  */
  79.                     my_ungetc(c);            /* unget EOF and return   */
  80.                     goto LINE_END;            /* what we've got.        */
  81.                 } else {                    /* if no chars on line,   */
  82.                     return FALSE;            /* return EOF status.      */
  83.                 }
  84.                 break;
  85.  
  86.               case '\t':                    /* expand tabs to 4 spaces */
  87.  
  88.                 do    {
  89.                     *pbuf++ = ' ';
  90.                     ++count;
  91.                 } while (count & 0x0003);
  92.                 break;
  93.  
  94.               case '\r':                    /* \r is end of line */
  95.  
  96.                 if ('\n' != (c = my_getc()))/* if next char is \n     */
  97.                     my_ungetc(c);            /* eat it, else unget it  */
  98.                 /* fall thru */
  99.  
  100.               case '\n':                    /* \n is end of line */
  101.  
  102.                 goto LINE_END;
  103.  
  104.               case 0x1A:                    /* ^Z may be at EOF */
  105.  
  106.                 if (MY_EOF == (c = my_getc())) { /* if we find ^Z at EOF */
  107.                     my_ungetc(c);                 /* we just suppress it, */
  108.                     goto LINE_END;                 /* if not at EOF, just  */
  109.                 }                                 /* output it like any     */
  110.                 /* fall thru */                  /* other ctl char.      */
  111.  
  112.               default:                        /* other ctl chars pass thru */
  113.  
  114.                 *pbuf++ = 0x7F;
  115.                 ++count;
  116.                 break;
  117.             }
  118.         }
  119.     }
  120.  
  121. LINE_END:
  122.  
  123.     *pbuf = '\0';
  124.     return TRUE;
  125. }
  126.  
  127. int brf_load(filename, linelist)
  128. /*****************************************************************************
  129.  *
  130.  ****************************************************************************/
  131.     char    *filename;
  132.     DlList    *linelist;
  133. {
  134.     short    status;
  135.     uchar    *workbuf;
  136.  
  137.     iobuf = NULL;
  138.     buf_alloc_size    = IOBUFSIZE;
  139.     buf_filled_size = 0;
  140.  
  141.     status = -39;
  142.  
  143.     do    {
  144.         if (NULL == (iobuf = DosMalloc(buf_alloc_size+2))) {
  145.             buf_alloc_size /= 2;
  146.             if (buf_alloc_size < IOBUFMIN) {
  147.                 goto ERROR_EXIT;
  148.             }
  149.         }
  150.     } while (NULL == iobuf);
  151.  
  152.     iobuf += 2; /* leave room at start of buffer for ungetc() */
  153.  
  154.     if (NULL == (workbuf = DosMalloc(MAX_LINE_LENGTH)))
  155.         goto ERROR_EXIT;
  156.  
  157.     if (0 > (status = fhandle = Fopen(filename, 0)))
  158.         goto ERROR_EXIT;
  159.     else
  160.         status = 0;
  161.  
  162.     while (build_line(workbuf)) {
  163.         brl_add(linelist, workbuf);
  164.     }
  165.  
  166. ERROR_EXIT:
  167.  
  168.     if (fhandle >= 0)
  169.         Fclose(fhandle);
  170.  
  171.     if (NULL != iobuf)
  172.         Mfree(iobuf-2); /* -2 compensates for padding we added above */
  173.  
  174.     if (NULL != workbuf)
  175.         Mfree(workbuf);
  176.  
  177.     return (br_errno = status);
  178. }
  179.  
  180. Browser *br_file(filename)
  181. /*****************************************************************************
  182.  *
  183.  ****************************************************************************/
  184.     char *filename;
  185. {
  186.     int     oldmouse;
  187.     DlList    linelist;
  188.     Browser *browser = NULL;
  189.  
  190.     linelist.head = linelist.tail = NULL;
  191.  
  192.     oldmouse = graf_mouse(BEE, NULL);
  193.  
  194.     if (0 <= brf_load(filename, &linelist)) {
  195.         browser = br_create(filename, &linelist, NULL);
  196.     }
  197.  
  198.     graf_mouse(oldmouse, NULL);
  199.  
  200.     return browser;
  201. }
  202.